package com.piesat.quake.business.station.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.piesat.common.utils.OkHttpUtil;
import com.piesat.quake.business.station.model.Csn;
import com.piesat.quake.business.station.mapper.CsnMapper;
import com.piesat.quake.business.station.service.CsnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-12-21
 */
@Service
public class CsnServiceImpl extends ServiceImpl<CsnMapper, Csn> implements CsnService {

    @Override
    public Page<Csn> findCsnList(Integer timeType, Integer gradeType, Integer page, Integer size) throws ParseException {
        if (timeType == 0 && gradeType == 0 && page == 1) {
            Long time = new Date().getTime();
            queryNetwork(2, 1, time);
        }
        LambdaQueryWrapper<Csn> wrapper = new LambdaQueryWrapper<>();
        if (timeType == 1) {
            //最近24小时内地震
            wrapper.ge(Csn::getOTime, DateUtils.addHours(new Date(), -24));
        } else if (timeType == 2) {
            //最近48小时内地震
            wrapper.ge(Csn::getOTime, DateUtils.addHours(new Date(), -48));
        } else if (timeType == 3) {
            //最近7天内地震
            wrapper.ge(Csn::getOTime, DateUtils.addDays(new Date(), -7));
        } else if (timeType == 4) {
            //最近30天内地震
            wrapper.ge(Csn::getOTime, DateUtils.addDays(new Date(), -30));
        } else if (timeType == 5) {
            //最近1年内地震
            wrapper.ge(Csn::getOTime, DateUtils.addYears(new Date(), -1));
        }

        if (gradeType == 1) {
            //6.0级以上地震
            wrapper.ge(Csn::getM, 6);
        } else if (gradeType == 2) {
            //5.0级以上地震
            wrapper.ge(Csn::getM, 5);
        } else if (gradeType == 3) {
            //4.0级以上地震
            wrapper.ge(Csn::getM, 4);
        } else if (gradeType == 4) {
            //3.0级以上地震
            wrapper.ge(Csn::getM, 3);
        } else if (gradeType == 5) {
            //3.0级以下地震
            wrapper.lt(Csn::getM, 3);
        }
        wrapper.orderByDesc(Csn::getOTime);
        return this.page(new Page<>(page, size), wrapper);
    }


    private void queryNetwork(int num, int page, long time) throws ParseException {
        String callback = "jQuery180007380936262547189_" + time;
        String result = OkHttpUtil.builder().url("http://www.ceic.ac.cn/ajax/speedsearch").addParam("num", 6).addParam("page", 1).addParam("callback", callback).addParam("_", time).get().sync();
        String json = result.substring(callback.length() + 1, result.length() - 1);
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray array = jsonObject.getJSONArray("shuju");
        for (int i = 0; i < array.size(); i++) {
            JSONObject item = array.getJSONObject(i);
            Csn csn = new Csn();
            csn.setId(item.getString("id"));
            csn.setCataId(item.getString("CATA_ID"));
            csn.setSaveTime(DateUtils.parseDate(item.getString("SAVE_TIME"), "yyyy-MM-dd HH:mm:ss"));
            csn.setOTime(DateUtils.parseDate(item.getString("O_TIME"), "yyyy-MM-dd HH:mm:ss"));
            csn.setEpiLat(item.getDouble("EPI_LAT"));
            csn.setEpiLon(item.getDouble("EPI_LON"));
            csn.setEpiDepth(item.getDouble("EPI_DEPTH"));
            csn.setAutoFlag(item.getString("AUTO_FLAG"));
            csn.setEqType(item.getString("EQ_TYPE"));
            csn.setOTimeFra(item.getString("O_TIME_FRA"));
            csn.setM(item.getDouble("M"));
            csn.setMMs(item.getDouble("M_MS"));
            csn.setMMs7(item.getDouble("M_MS7"));
            csn.setMMl(item.getDouble("M_ML"));
            csn.setMMb(item.getDouble("M_MB"));
            csn.setMMb2(item.getDouble("M_MB2"));
            csn.setSumStn(item.getDouble("SUM_STN"));
            csn.setLocStn(item.getDouble("LOC_STN"));
            csn.setLocationC(item.getString("LOCATION_C"));
            csn.setLocationS(item.getString("LOCATION_S"));
            csn.setCataType(item.getString("CATA_TYPE"));
            csn.setSyncTime(DateUtils.parseDate(item.getString("SYNC_TIME"), "yyyy-MM-dd HH:mm:ss"));
            csn.setIsDel(item.getString("IS_DEL"));
            csn.setEqCataType(item.getString("EQ_CATA_TYPE"));
            csn.setNewDid(item.getString("NEW_DID"));
            this.saveOrUpdate(csn);
        }
    }
}
